package com.deschen.security.core.social;

import com.deschen.security.core.properties.SecurityProperties;
import com.sun.org.apache.xml.internal.security.encryption.EncryptedType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.social.UserIdSource;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurer;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionSignUp;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.social.security.AuthenticationNameUserIdSource;
import org.springframework.social.security.SpringSocialConfigurer;

import javax.sql.DataSource;
import javax.swing.*;

/**
 * @Author deschen
 * @Create 2019/9/2
 * @Description social配置类
 * @Since 1.0.0
 */
@Configuration
@EnableSocial  //开启social
@Order(1)
public class SocialConfig extends SocialConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private SecurityProperties securityProperties;

    @Autowired(required = false)
    private ConnectionSignUp connectionSignUp;


    /**
     * 获取第三方登入用户信息处理类
     * @param connectionFactoryLocator 根据条件获取connectfactory
     * @return
     */
    @Override
    public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {

        // connectionFactoryLocator 根据条件查找相应connectionFactory来构建connection数据
        // 用户信息加密工具  Encryptors.noOpText() 对access_token加密方式：未加密
        CustomJdbcUsersConnectionRepository customJdbcUsersConnectionRepository =
                new CustomJdbcUsersConnectionRepository(dataSource, connectionFactoryLocator, Encryptors.noOpText());
//        customJdbcUsersConnectionRepository.setTablePrefix("blog_");

        // 自动注册
        if (connectionSignUp != null) {
            customJdbcUsersConnectionRepository.setConnectionSignUp(connectionSignUp);
        }
        return customJdbcUsersConnectionRepository;
    }

    /**
     * 第三方认证拦截链接
     * @return
     */
    @Bean
    public SpringSocialConfigurer customSpringSocialConfigurer() {
        CustomSpringSocialConfigurer customSpringSocialConfigurer = new CustomSpringSocialConfigurer(
                securityProperties.getSocial().getFilterProcessesUrl(),
                securityProperties.getBrowser().getSignupUrl());
        return customSpringSocialConfigurer;
    }

    @Override
    public UserIdSource getUserIdSource() {
        return new AuthenticationNameUserIdSource();
    }

    /**
     * 功能：1注册过程中获取spring social信息；
     * 2注册完成，把用户id传给spring social
     * @return
     */
    @Bean
    public ProviderSignInUtils providerSignInUtils(
            ConnectionFactoryLocator connectionFactoryLocator) {
        return new ProviderSignInUtils(connectionFactoryLocator, getUsersConnectionRepository(connectionFactoryLocator));
    }
}
